let mysql = require('mysql2/promise');
let config = require('../config');
config.database.queryFormat = function(query, values) {
  if (!values) return query;
  return query.replace(/\:(\w+)/g, function(txt, key) {
    if (values.hasOwnProperty(key)) {
      return this.escape(values[key]);
    }
    return txt;
  }.bind(this));
};

var pool = mysql.createPool(config.database);

exports.execute = async function(sql, params) {
  if (!sql) {
    return;
  }
  let connect = await pool.getConnection();

  let data = await connect.execute(sql,params);
  connect.release();
  return data;
}
exports.query = async function(sql, params) {
  try{
  if (!sql) {
    return;
  }
  // let connect = await pool.getConnection();
  // //测试服务器心跳是否关闭
  // connect.ping(function (err) {
  //   if (err) throw err;
  //   console.log('Server responded to ping');
  // })
  // let data = await connect.query(sql,params);
  // connect.release();
  // return data;

  
  // mysql 短连接
let connect = await mysql.createConnection(config.database);
let data = await connect.query(sql, params);
connect.end();
return data;
}
catch (error) {
  console.log(error) 
 }
}
